#if UNITY_EDITOR

using UnityEngine;
using System.Linq;

namespace IQIGame.Onigao.Framework
{

    public class EditorPreferenceVector3 : PreferenceBase<Vector3>
    {
        public EditorPreferenceVector3(string key, Vector3 defaultValue) : base(key, defaultValue)
        {
        }

        protected override Vector3 ReadValue(Vector3 defaultValue)
        {
            return StringToVector3(UnityEditor.EditorPrefs.GetString(key, string.Empty), ref defaultValue);
        }

        protected override void SaveValue(Vector3 value)
        {
            UnityEditor.EditorPrefs.SetString(key, Vector3ToString(value));
        }

        public static Vector3 StringToVector3(string content, ref Vector3 defaultValue)
        {
            if (string.IsNullOrEmpty(content))
            {
                return defaultValue;
            }
            var arr = content.Split('|');
            if (arr.Length != 3)
            {
                return defaultValue;
            }
            return new Vector3(float.Parse(arr[0]), float.Parse(arr[1]), float.Parse(arr[2]));
        }

        public static string Vector3ToString(Vector3 value)
        {
            return value.x + "|" + value.y + "|" + value.z;
        }
    }
}

#endif